<?php
include_once("functions/db_manipulate.php");

if (!init()) {
    redirect("login.php");
}

$imageResultSet = getGalleryImg($_GET['iid']);

if ($_GET['page'] == '') {
    $start = 1;
    $page = 1;
} else {
    $start = ($_GET['page'] - 1) * 20 + 1;
    $page = $_GET['page'];
}

$imageCommentsSet = getGalleryImgComments($start, $start + 19, $_GET['iid']);

$msg = countGalleryImageComments($_GET['iid']);
$pages = pagesFromMessages($msg);
$pagesStart = 0;
$pagesEnd = 0;

$page = $pages;

if ($pages <= 5) {
    $pagesStart = 1;
    $pagesEnd = $pages;
} else if ($pages >= 5 && $page + 2 <= $pages) {
    $pagesStart = $page - 2;
    $pagesEnd = $page + 2;
} else if ($pages >= 5 && $page + 2 > $pages) {
    $pagesStart = $pages - 4;
    $pages;
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Изображение</title>

    <link rel="stylesheet" type="text/css" href="css/navigation.css">
    <link rel="stylesheet" type="text/css" href="css/square.css">
    <link rel="stylesheet" type="text/css" href="css/menu.css">
    <link rel="stylesheet" type="text/css" href="css/common.css">
    <link rel="stylesheet" type="text/css" href="css/gallery.css">
    <link rel="stylesheet" type="text/css" href="css/comment.css">
    <script src="resources/jquery.js"></script>

    <meta charset="UTF-8">
    <script language="JavaScript">
        function reply(i) {
            $('#reply').val(i);
            $('#reply2').val(i);
            $('#textarea').focus();
            $('#textarea').attr("placeholder", "Вы процитировали сообщение, введите ваш ответ.. ")
        }

        function customFormSubmit() {
            $('#maximized_navigation').show();
            $('#commentform').submit();
        }
    </script>
</head>


<body class="body">

<?php
include_once("menu.php");
?>

<div id="square">
<?php
if ($image = mysql_fetch_assoc($imageResultSet)) {
    ?>
    <div class="fulldiv">
        <div class="imageheader">
            <?= htmlspecialchars($image['header']) ?>
        </div>
        <div class="fullimgdiv">
            <img src="gallery/<?= $image['path'] ?>" class="fullimg">
        </div>
        <div class="imglegenddiv">
            <?= htmlspecialchars($image['comment']) ?>
        </div>
        <div class="fullimgdiv">
            <div id="imginfo">
                <div id="author">
                    <?= $image['author'] ?>
                </div>
                <div id="date">
                    <?= $image['date'] ?>
                </div>
                <div id="date">
                    <?= $image['time'] ?>
                </div>
            </div>
        </div>
        <div class="manipulationdiv">
            <div class="manipulationbutton" style="margin-left: 540px"
                <?php if (!isLiked("i", $image['id'])) {
                    ?>
                    onclick="location.href='image_like_handler.php?iid=<?= $image['id'] ?>&like=1'"
                <?php
                }
                ?>
                >
                +(<?= $image['likes'] ?>)
            </div>
            <div class="manipulationbutton"
                <?php if (!isDisiked("i", $image['id'])) {
                    ?>
                    onclick="location.href='image_like_handler.php?iid=<?= $image['id'] ?>&dislike=1'"
                <?php
                }
                ?>
                >
                -(<?= $image['dislikes'] ?>)
            </div>
        </div>
    </div>

    <div class="commentsheader">
        Комментарии пользователей:
    </div>

    <div id="pages">
        <?php


        if ($pages == 0) {
        }

        if ($page > 1) {
            ?>
            <div class="page"
                 onclick="location.href='image.php?tid=<?= $topic['id'] ?>&page=1'">
                <<
            </div>
        <?php
        }
        for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
            ?>
            <div class="page"
                 onclick="location.href='image.php?tid=<?= $topic['id'] ?>&page=<?= $i ?>'">
                <?= $i ?>
            </div>
        <?php
        }
        if ($page != $pages) {
            ?>
            <div class="page"
                 onclick="location.href='image.php?tid=<?= $topic['id'] ?>&page=<?= $pages ?>'">
                >>
            </div>
        <?php
        }
        ?>
    </div>


    <?php
    while ($commentRow = mysql_fetch_assoc($imageCommentsSet)) {
        ?>
        <div class="comment">
            <div class="commentinfodiv">
                <div class="commentauthor">
                    #<?= $commentRow['id'] ?>
                </div>
                <div class="commentdate">
                    <?= $commentRow['date'] ?>
                </div>
                <div class="commentdate">
                    <?= $commentRow['time'] ?>
                </div>
            </div>

            <div>
                <div class="avatardiv">
                    <div onclick="$('#userinfo<?= $commentRow['id'] ?>').slideToggle('slow')">
                        <img src="user_img/<?= $commentRow['img'] ?>" class="avatarimg">
                    </div>
                    <div class="useractions" id="userinfo<?= $commentRow['id'] ?>">
                        <div class="userbutton" onclick="location.href='profile.php?uid=<?= $commentRow['uid'] ?>'">
                            Профиль
                        </div>
                        <?php
                        if ($commentRow['uid'] != $_SESSION['id']) {
                            ?>
                            <div class="userbutton"
                                 onclick="location.href='conversation_check.php?uid=<?= $commentRow['uid'] ?>'">
                                Сообщение
                            </div>
                        <?php
                        }
                        ?>
                    </div>
                    <div class="userinfo">
                        <div>
                            Ник: <?= getTextWithAcronym($commentRow['author'], 15) ?>
                        </div>
                        <div>
                            Регистрация: <?= $commentRow['registered'] ?>
                        </div>
                        <div>
                            Сообщений: <?= $commentRow['msg'] ?>
                        </div>
                        <div>
                            Почта: <?= getTextWithAcronym($commentRow['email'], 22) ?>
                        </div>
                    </div>
                </div>

                <div class="commentdiv">
                    <?php if ($commentRow['quote'] != 0) { ?>
                        <div class="commentquote">
                            <?php
                            $quoteRow = mysql_fetch_assoc(getGalleryComment($commentRow['quote']));
                            ?>
                            <?= htmlspecialchars($quoteRow['content']); ?>
                        </div>
                    <?php
                    }
                    echo htmlspecialchars($commentRow['content']) ?>
                </div>
            </div>

            <div class="manipulationdiv" style="<?php if (!init()) echo "display: none;" ?>">
                <div class="manipulationbutton"
                    <?php if (!isLiked("g", $commentRow['id'])) {
                        ?>
                        onclick="location.href = 'image_comment_like_handler.php?cid=<?= $commentRow['id'] ?>&like=1'"
                    <?php
                    }
                    ?>
                    >
                    +(<?= $commentRow['likes'] ?>)
                </div>
                <div class="manipulationbutton"
                    <?php if (!isDisiked("g", $commentRow['id'])) {
                        ?>
                        onclick="location.href = 'image_comment_like_handler.php?cid=<?= $commentRow['id'] ?>&dislike=1'"
                    <?php
                    }
                    ?>
                    >
                    -(<?= $commentRow['dislikes'] ?>)
                </div>
                <?php if (!isSpammed("g", $commentRow['id'])) { ?>
                    <div class="manipulationbutton"
                         onclick="location.href = 'image_comment_spam_handler.php?cid=<?= $commentRow['id'] ?>'">
                        Спам
                    </div>
                <?php
                }
                ?>
                <a href="#anchor">
                    <div class="manipulationbutton"
                         style="margin-left: 160px;"
                         onclick="reply('<?= $commentRow['id'] ?>')">
                        Ответить
                    </div>
                </a>
            </div>

        </div>

    <?php
    }
    ?>

    <div id="pages">
        <?php


        if ($pages == 0) {
        }

        if ($page > 1) {
            ?>
            <div class="page"
                 onclick="location.href='image.php?tid=<?= $topic['id'] ?>&page=1'">
                <<
            </div>
        <?php
        }
        for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
            ?>
            <div class="page"
                 onclick="location.href='image.php?tid=<?= $topic['id'] ?>&page=<?= $i ?>'">
                <?= $i ?>
            </div>
        <?php
        }
        if ($page != $pages) {
            ?>
            <div class="page"
                 onclick="location.href='image.php?tid=<?= $topic['id'] ?>&page=<?= $pages ?>'">
                >>
            </div>
        <?php
        }
        ?>
    </div>
<?php
}
?>

<a id="anchor">

    <div class="common">
        <form enctype="multipart/form-data" accept-charset="UTF-8" action="image_comment_handler.php" method="post"
              id="commentform">
            <span style="font-weight: bold">Новый комментарий:</span>
            <textarea name="content" id="textarea" rows="11" placeholder="Введите Ваш комментарий.."></textarea>

            <div style="display: inline-block; width: 100%; padding-left: 500px;">
                <div id="commentbutton" onclick="customFormSubmit()">Продолжить</div>
            </div>

            <input type="hidden" name="iid" value="<?= $_GET['iid'] ?>">
            <input type="hidden" name="reply" id="reply">
        </form>
    </div>
</a>


</div>

<?php
include_once("navigation.html");
?>

</body>
</html>